package org.ix.net.controllers;

import org.ix.net.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ix.data.entities.User;

public class Login extends BaseLocalServlet {

    public String getServletInfo() {
        return "Login servlet";
    }

    protected void serve(HttpServletRequest req, HttpServletResponse res) {
        String username = nvc(req.getParameter("username"));
        String password = nvc(req.getParameter("password"));
        if (username.length() > 0 && password.length() > 0) {
            User user = new User(username);
            req.getSession().setAttribute("user", user);
            res.setHeader("refresh", "0");
        } else {
            out.println("<h3>login</h3>");
            out.println("<form method=\"post\">");
            out.println("<input type=\"text\" name=\"username\" value=\"" + username + "\" />");
            out.println("<br/>");
            out.println("<input type=\"text\" name=\"password\" value=\"" + password + "\" />");
            out.println("<br/>");
            out.println("<input type=\"submit\" />");
            out.println("</form>");
        }
        out.println("</body>");
        out.println("</html>");
    }
}
